package com.dg.river.wxapi;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.dg.river.contant.CommonCode;
import com.dg.river.contant.Constant;
import com.dg.river.module.common.bean.EbMessage;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * @作者：Hututu
 * @时间：2023/6/7 18:44
 * @描述：内容描述
 * @修改：修改内容
 * @Version:1.0
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IWXAPI api = WXAPIFactory.createWXAPI(WXPayEntryActivity.this, null);
        api.handleIntent(getIntent(), WXPayEntryActivity.this);
        // 将应用的appid注册到微信
        api.registerApp(Constant.WEXIN_KEY);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        int type = baseResp.getType();
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (type == ConstantsAPI.COMMAND_PAY_BY_WX) { // 微信支付完成
                    EventBus.getDefault().post(new EbMessage(CommonCode.EventBus.WECHAT_PAY_SUCCESS, null));
                }
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED: // 授权拒绝
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL: // 授权取消
                finish();
                break;
        }
    }
}
